package com.sam.chat.controller;


import com.sam.chat.api.CommonResult;
import com.sam.chat.service.IChatUserService;
import com.sam.chat.vo.UserVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 用户管理
 * </p>
 *
 * @author sam
 * @since 2022-12-31
 */
@Tag(name = "用户管理")
@RestController
@RequestMapping("/chatUser")
@RequiredArgsConstructor
public class ChatUserController {
    private final IChatUserService iChatUserService;

    /**
     * 登录
     */
    @Operation(summary = "密码登录")
    @PostMapping("/loginByPassword")
    public CommonResult<UserVo> loginByPassword(String username, String password) {
        UserVo userVo = iChatUserService.loginByPassword(username, password);
        return new CommonResult<>(200, "success", userVo);
    }

    /**
     * 注冊
     */
    @Operation(summary = "注冊")
    @PostMapping("/register")
    public CommonResult<Boolean> register(String username, String nickname, String password, String phone, String header, Integer sex,Integer age) {
        if (!iChatUserService.checkUsername(username)) {
            return new CommonResult<>(202, "用户名重复了",false);
        }
        if (phone!=null&&!phone.isEmpty()&&!iChatUserService.checkPhone(phone)) {
            return new CommonResult<>(202, "手机号重复了",false);
        }
        if (age == null){
            age =0;
        }

        boolean result = iChatUserService.register(username, nickname, password, phone, header, sex,age);
        if (result) {
            return new CommonResult<>(200, "success", true);
        } else {
            return new CommonResult<>(200, "未知错误",false);
        }
    }

}

